//package com.cupdata.marathon.component;
//
//import cn.hutool.core.util.StrUtil;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.cupdata.marathon.client.AcctQueryService;
//import com.cupdata.marathon.common.RedisService;
//import com.cupdata.marathon.definition.OperQueryRslt;
//import com.cupdata.marathon.definition.RspCd;
//import com.cupdata.marathon.entity.TblRetryTransLog;
//import com.cupdata.marathon.param.acctquery.AcctQueryRequest;
//import com.cupdata.marathon.param.acctquery.AcctQueryResponse;
//import com.cupdata.marathon.service.ITblHackCouponDownTransLogService;
//import com.cupdata.marathon.service.ITblRetryTransLogService;
//import lombok.extern.slf4j.Slf4j;
//import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
///**
// * 轮询重试流水表
// * 调用账务查询接口
// * 更新领取流水表
// * 删除重试流水表
// * 释放优惠券到优惠券池
// * 删除用户领取的券
// */
//@Slf4j
//@Component
//public class RetryTransLogCheck {
//
//    @Autowired
//    private ITblHackCouponDownTransLogService downTransLogService;
//
//    @Autowired
//    private ITblRetryTransLogService retryTransLogService;
//
//    @Autowired
//    private RedisService redisService;
//
//    @Autowired
//    private AcctQueryService acctQueryService;
//
//    // 每十秒执行一次
////    @Scheduled(cron = "0/10 * * * * ?")
////    @SchedulerLock(name = "retry_check_lock", lockAtLeastFor = "PT5S", lockAtMostFor = "PT10S")
//    public void check() {
//        log.info("轮询线程为：" + Thread.currentThread().getName());
//
//        Page<TblRetryTransLog> page = retryTransLogService.page(new Page<>());
//        if (page.getSize() > 0) {
//            page(page);
//        }
//    }
//
//    // 轮询每一页的记录
//    private void page(Page<TblRetryTransLog> page) {
//        page.getRecords().forEach(r -> {
//            // 调用动账查询接口
//            AcctQueryRequest acctQueryRequest = new AcctQueryRequest();
//            acctQueryRequest.setSysId(r.getSysId());
//            acctQueryRequest.setAccessId(r.getAccessId());
//            acctQueryRequest.setAcctTp(r.getAcctTp());
//            acctQueryRequest.setTransId(r.getTransId());
//            acctQueryRequest.setUserId(r.getUserId());
//            acctQueryRequest.setCouponId(r.getCouponId());
//            AcctQueryResponse acctQueryResponse = acctQueryService.execute(acctQueryRequest);
//            if (RspCd.SUCCESS.getCode().equals(acctQueryResponse.getRspCd())) {
//                if (StrUtil.equalsAny(acctQueryResponse.getOperRslt(),
//                        OperQueryRslt.SUCCESS.getCode(),
//                        OperQueryRslt.FAIL.getCode(),
//                        OperQueryRslt.ILLEGAL.getCode())) {
//                    execute(r, acctQueryResponse);
//                }
//            }
//        });
//    }
//
//    private void execute(TblRetryTransLog record, AcctQueryResponse response) {
//        // 更新领取流水表
//        downTransLogService.update(record.getCouponId(), response);
//        // 删除重试流水表
//        retryTransLogService.removeById(record.getCouponId());
//
//        if (!OperQueryRslt.SUCCESS.getCode().equals(response.getOperRslt())) {
//            // todo 操作是否可以合并
//            // 增加优惠券总数
//            // 删除该用户领取的券
//            redisService.recoverCoupon(record.getUserId(), record.getCouponTp());
//        }
//    }
//}
